﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.Common;
using System.Linq;
using HoangPhap.EntityClasses;

namespace HoangPhap.Bussiness
{
    public class Article
    {
        public List<EntityClasses.Article> SelectAll()
        {
            var lst = new List<EntityClasses.Article>();
            var ct = new Data.HoangPhapDataContext().GetArticlesSelectAllResults();
            if (ct.Rows.Count > 0)
            {
                lst.AddRange(from DataRow dt in ct.Rows
                             select new EntityClasses.Article
                             {
                                 Id = Convert.ToInt32(dt["ID"].ToString()),
                                 ArticleCategoryId = Convert.ToInt32(dt["ArticleCategoryId"].ToString()),
                                 Order = Convert.ToInt32(dt["Order"].ToString()),
                                 ImageSource = dt["ImageSource"].ToString(),
                                 CharityId = Convert.ToInt32(dt["CharityId"].ToString()),
                                 CreatedDate = Convert.ToDateTime(dt["CreatedDate"].ToString()),
                                 EditorId = Convert.ToInt32(dt["EditorId"].ToString()),
                                 PagodaId = Convert.ToInt32(dt["PagodaId"].ToString()),
                                 RequestId = Convert.ToInt64(dt["RequestId"].ToString()),
                                 CharityProgramId = Convert.ToInt32(dt["CharityProgramId"].ToString()),
                                 Status = Convert.ToInt32(dt["Status"].ToString()),
                                 UpdatedDate = Convert.ToDateTime(dt["UpdatedDate"].ToString())
                             });
            }
            return lst;   
        }

        public EntityClasses.Article SelectById(int id)
        {
            var lst = new EntityClasses.Article();
            var ct = new Data.HoangPhapDataContext().GetArticlesSelectByIdResults(id);
            if (ct.Rows.Count <= 0) return lst;

            lst = new EntityClasses.Article
            {
                Id = Convert.ToInt32(ct.Rows[0]["ID"].ToString()),
                ArticleCategoryId = Convert.ToInt32(ct.Rows[0]["ArticleCategoryId"].ToString()),
                Order = Convert.ToInt32(ct.Rows[0]["Order"].ToString()),
                ImageSource = ct.Rows[0]["ImageSource"].ToString(),
                CharityId = Convert.ToInt32(ct.Rows[0]["CharityId"].ToString()),
                CreatedDate = Convert.ToDateTime(ct.Rows[0]["CreatedDate"].ToString()),
                EditorId = Convert.ToInt32(ct.Rows[0]["EditorId"].ToString()),
                PagodaId = Convert.ToInt32(ct.Rows[0]["PagodaId"].ToString()),
                RequestId = Convert.ToInt64(ct.Rows[0]["RequestId"].ToString()),
                CharityProgramId = Convert.ToInt32(ct.Rows[0]["CharityProgramId"].ToString()),
                Status = Convert.ToInt32(ct.Rows[0]["Status"].ToString()),
                UpdatedDate = Convert.ToDateTime(ct.Rows[0]["UpdatedDate"].ToString())
            };
            return lst;
        }

        public List<EntityClasses.Article> SelectBySystemSubAdminId(int systemSubAdminId)
        {
            var lst = new List<EntityClasses.Article>();
            var ct = new Data.HoangPhapDataContext().GetArticlesSelectBySystemSubAdminIdResults(systemSubAdminId);
            if (ct.Rows.Count > 0)
            {
                lst.AddRange(from DataRow dt in ct.Rows
                             select new EntityClasses.Article
                             {
                                 Id = Convert.ToInt32(dt["ID"].ToString()),
                                 ArticleCategoryId = Convert.ToInt32(dt["ArticleCategoryId"].ToString()),
                                 Order = Convert.ToInt32(dt["Order"].ToString()),
                                 ImageSource = dt["ImageSource"].ToString(),
                                 CharityId = Convert.ToInt32(dt["CharityId"].ToString()),
                                 CreatedDate = Convert.ToDateTime(dt["CreatedDate"].ToString()),
                                 EditorId = Convert.ToInt32(dt["EditorId"].ToString()),
                                 PagodaId = Convert.ToInt32(dt["PagodaId"].ToString()),
                                 RequestId = Convert.ToInt64(dt["RequestId"].ToString()),
                                 CharityProgramId = Convert.ToInt32(dt["CharityProgramId"].ToString()),
                                 Status = Convert.ToInt32(dt["Status"].ToString()),
                                 UpdatedDate = Convert.ToDateTime(dt["UpdatedDate"].ToString())
                             });
            }
            return lst;
        }

        public int DeleteById(int id)
        {
            return new Data.HoangPhapDataContext().CallArticlesDeleteById(id);
        }

        public int DeleteBySystemSubAdminId(int id)
        {
            return new Data.HoangPhapDataContext().CallArticlesDeleteBySystemSubAdminId(id);
        }

        public int DeleteDynamic(System.String whereCondition)
        {
            return new Data.HoangPhapDataContext().CallArticlesDeleteDynamic(whereCondition);
        }

        public int Insert(System.Int32 articleCategoryId, System.Int32 order, System.String imageSource, 
            System.DateTime createdDate, System.DateTime updatedDate, System.Int32 systemSubAdminId, 
            System.Int32 pagodaId, System.Int32 editorId, System.Int32 charityProgramId, System.Int32 charityId, 
            System.Int32 status, System.Int64 requestId, ref System.Int32 id)
        {
            return new Data.HoangPhapDataContext().CallArticlesInsert(articleCategoryId, order, imageSource, createdDate,
                updatedDate, systemSubAdminId, pagodaId, editorId, charityProgramId, charityId, status, requestId,
                ref id);
        }

        public int InsertUpdate(System.Int32 id, System.Int32 articleCategoryId, System.Int32 order, 
            System.String imageSource, System.DateTime createdDate, System.DateTime updatedDate, 
            System.Int32 systemSubAdminId, System.Int32 pagodaId, System.Int32 editorId, 
            System.Int32 charityProgramId, System.Int32 charityId, System.Int32 status, System.Int64 requestId)
        {
            return new Data.HoangPhapDataContext().CallArticlesInsertUpdate(id, articleCategoryId, order, 
                imageSource, createdDate, updatedDate, systemSubAdminId, pagodaId, editorId, 
                charityProgramId, charityId, status, requestId);
        }

        public int Update(System.Int32 id, System.Int32 articleCategoryId, System.Int32 order, 
            System.String imageSource, System.DateTime createdDate, System.DateTime updatedDate, 
            System.Int32 systemSubAdminId, System.Int32 pagodaId, System.Int32 editorId, 
            System.Int32 charityProgramId, System.Int32 charityId, System.Int32 status, System.Int64 requestId)
        {
            return new Data.HoangPhapDataContext().CallArticlesUpdate(id, articleCategoryId, order,
                imageSource, createdDate, updatedDate, systemSubAdminId, pagodaId, editorId,
                charityProgramId, charityId, status, requestId);
        }
    }
}
